//
//  SafeDangerViewController.m
//  Estate
//
//  Created by wangdongdong on 2017/9/29.
//  Copyright © 2017年 ttsx. All rights reserved.
//

#import "SafeDangerModel.h"
#import "SafeDangerCell.h"

#import "SafeDangerViewController.h"

@interface SafeDangerViewController ()<UITableViewDelegate, UITableViewDataSource>

// 表格
@property (strong, nonatomic) TPKeyboardAvoidingTableView *tableView;

// 数据源
@property (strong, nonatomic) NSMutableArray *dataArr;
// 检查类型
@property (strong, nonatomic) NSArray *checkTypeArr;
// 检查类型名称
@property (strong, nonatomic) NSArray *checkNameArr;


@end


@implementation SafeDangerViewController

// 检查类型
- (NSArray *)checkTypeArr {
    if (!_checkTypeArr) {
        _checkTypeArr = @[@"CHECK_01", @"CHECK_02", @"CHECK_03", @"CHECK_04", @"CHECK_05", @"CHECK_06", @"CHECK_07", @"CHECK_08", @"CHECK_09", @"CHECK_10", @"CHECK_11"];
    }
    return _checkTypeArr;
}

// 检查类型名称
- (NSArray *)checkNameArr {
    if (!_checkNameArr) {
        _checkNameArr = @[@"专项施工方案编制及审查情况", @"安全技术交底落实情况", @"工程建设强制性标准（条文）执行情况", @"危险部位安全警示标志及防护设施情况", @"施工现场临时用电安全措施落实情况", @"施工现场危险品使用安全措施落实情况", @"机械设备定期检查、维修、保养情况", @"特种作业人员持证上岗情况", @"作业人员个人防护用品佩戴情况", @"施工专职安全员到岗监督检查情况", @"其它施工安全措施落实情况"];
    }
    return _checkNameArr;
}

// 数据源
- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
        for (int i = 0; i < 11; i++) {
            SafeDangerModel *model = [[SafeDangerModel alloc] init];
            // 检查类型名称
            model.checkTypeName = self.checkNameArr[i];
            // 检查类型
            model.checkType = self.checkTypeArr[i];
            // 监理评价(是否符合要求)
            model.supervisionEvaluation = @"1";
            [_dataArr addObject:model];
        }
    }
    return _dataArr;
}

- (TPKeyboardAvoidingTableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[TPKeyboardAvoidingTableView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT - 188) style:UITableViewStylePlain];
        _tableView.backgroundColor = UIColorFromRGB(0xf2f2f2);
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.bounces = NO;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        [_tableView registerNib:[UINib nibWithNibName:@"SafeDangerCell" bundle:nil] forCellReuseIdentifier:@"dangerCell"];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithRed:0.97 green:0.97 blue:0.97 alpha:1.00];
    [self.view addSubview:self.tableView];
}

// 添加通知 (暂存/提交检查信息)
- (void)viewWillAppear:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveSupervisionSafetyInspection:) name:@"saveSafeCheck" object:nil];
}

// 移除通知 (暂存/提交检查信息)
- (void)viewWillDisappear:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"saveSafeCheck" object:nil];
}

#pragma mark - 提交保存监理安全巡查
- (void)saveSupervisionSafetyInspection:(NSNotification *)notification {
    // 接收通知来的信息
    NSDictionary *parameDic = [notification object];
    FMDatabase *db = [FMDatabase databaseWithPath:DEF_DB_DATABASEPATH];
    DBHelper *dbhelper = [DBHelper help];
    
    for (SafeDangerModel *model in self.dataArr) {
        // 字段及对应值
        NSDictionary *pragameDic = [NSDictionary dictionary];
        pragameDic = @{
                       @"ID":DEFISNUL([NSUUID UUID].UUIDString),
                       @"PRO_ID":DEF_PERSISTENT_GET_OBJECT(@"proid"),
                       @"BASIC_RECORD_ID":DEFISNUL(parameDic[@"safeBaseID"]),
                       @"RECORD_USER":DEFISNUL(DEF_PERSISTENT_GET_OBJECT(@"userid")),
                       @"RECORD_TYPE":DEFISNUL(@"SAFE_RECORD_03"),
                       @"RECORD_TITLE":DEFISNUL(parameDic[@"title"]),
                       @"RECORD_NUMBER":DEFISNUL(parameDic[@"recordNumber"]),
                       @"CHECK_NAME":DEFISNUL(model.checkTypeName),
                       @"CHECK_TYPE":DEFISNUL(model.checkType),
                       @"CHECK_INFO":DEFISNUL(model.checkCase),
                       @"IS_ON_POSITION":DEFISNUL(model.supervisionEvaluation),
                       @"EXTEND_INFO":@"",
                       @"CREATE_USER_NAME":DEF_PERSISTENT_GET_OBJECT(@"loginuser"),
                       @"CREATE_TIME":DEFISNUL([LSUtility nowTime]),
                       @"CREATE_IP":DEFISNUL([LSUtility getIPAddress]),
                       @"MODIFY_USER_NAME":@"",
                       @"MODIFY_TIME":@"",
                       @"MODIFY_IP":@"",
                       @"LOCAL_MODIFY_USER_NAME":@"",
                       @"LOCAL_MODIFY_TIME":@"",
                       @"LOCAL_MODIFY_STATE":@"ADD",
                       };
        
        BOOL isAdd = [dbhelper DataBase:db insertKeyValues:pragameDic intoTable:SAFE_DANGER];
        if (isAdd) {
            NSLog(@"添加危险性较大工程监理检查成功");
            
        } else {
            NSLog(@"添加危险性较大工程监理检查失败");
        }
    }
    
    // 刷新安全列表页
    [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshSafeList" object:nil];
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma MARK - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    return nil;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 190;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSInteger section = indexPath.section;
    NSInteger row = indexPath.row;
    // 数据
    SafeDangerModel *model = self.dataArr[section];
    
    SafeDangerCell *dangerCell = [tableView dequeueReusableCellWithIdentifier:@"dangerCell" forIndexPath:indexPath];
    dangerCell.selectionStyle = UITableViewCellSelectionStyleNone;
    // 检查类型名称
    dangerCell.nameLabel.text = model.checkTypeName;
    
    // 检查情况
    if (model.checkCase && ![model.checkCase isEqualToString:@""]) {
        dangerCell.describeText.text = model.checkCase;
        
    } else {
        dangerCell.describeText.text = nil;
    }
    dangerCell.checkSituationBlock = ^(NSString *str) {
        model.checkCase = str;
    };
    
    // 监理评价(是否符合要求)
    if (model.supervisionEvaluation && ![model.supervisionEvaluation isEqualToString:@""]) {
        if ([model.supervisionEvaluation isEqualToString:@"1"]) {
            dangerCell.conformSwitch.on = YES;
            
        } else {
            dangerCell.conformSwitch.on = NO;
        }
    }
    dangerCell.supervisionEvaluationBlock = ^(NSString *str) {
        model.supervisionEvaluation = str;
    };
    return dangerCell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

